package org.example.service.leave;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class LeaveServiceImpl implements LeaveService {

    // 复杂工作流的进程ID
    private final static String ProcessInstanceByKey = "leaveProcess";

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    HistoryService historyService;

    @Override
    public String startProcess(String orderNo) {
        // 启动一个流程(businessKet业务单号)
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(ProcessInstanceByKey, orderNo);
        log.info("processInstance.getId()======={}", processInstance.getId());
        return processInstance.getId() + " success";
    }

    @Override
    public String askForLeave(String taskId) {
        Map<String, Object> map = new HashMap<>(16);
        map.put("leaveDays", 7);
        taskService.complete(taskId, map);
        return "发起请假success";
    }

    @Override
    public String projectReview(String taskId, String projectFlag, String reason) {
        Map<String, Object> map = new HashMap<>(16);
        map.put("projectFlag", projectFlag);
        map.put("reason", reason);
        // 项目经理审核确认
        taskService.complete(taskId, map);
        return "项目经理审核 " + projectFlag;
    }

    @Override
    public String departmentReview(String taskId, String departFlag, String reason) {
        Map<String, Object> map = new HashMap<>(16);
        map.put("departFlag", departFlag);
        map.put("reason", reason);
        // 部门经理审核确认
        taskService.complete(taskId, map);
        return "部门经理审核 " + departFlag;
    }

    @Override
    public String editOrder(String taskId, int leaveDays) {
        Map<String, Object> map = new HashMap<>(16);
        map.put("leaveDays", leaveDays);
        taskService.complete(taskId, map);
        return "修改单据success";
    }

    @Override
    public String confirmOrder(String taskId) {
        Map<String, Object> map = new HashMap<>(16);
        taskService.complete(taskId, map);
        return "登记确认success";
    }
}
